package com.zhq.okhttp;



import com.zhq.apputil.utils.LogUtil;
import com.zhq.okhttp.fastjsonconverter.FastJsonConvertFactory;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * 类描述：封装一个retrofit集成0kHttp3的抽象基类
 */
public abstract class RetrofitUtils {
    //服务器路径
    //public static String API_SERVER = "http://47.98.241.242:9001/";//29  :8888 2.62 0.117  47.96.98.213:9001
    //public static  String API_SERVER = "http:/192.168.2.105:8080/";
    public static String API_SERVER = "http://192.168.2.121:18114";//http://192.168.2.121:18114

    private static Retrofit mRetrofit;
    private static OkHttpClient mOkHttpClient;

    public static void setApiIp(String ip) {
        API_SERVER = ip;
        if (!LogUtil.isDEBUG()) {
            LogUtil.e("ipAddress:" + API_SERVER);
            if (null == mOkHttpClient) {
                mOkHttpClient = OkHttp3Utils.getOkHttpClient();
            }
            mRetrofit = new Retrofit.Builder()
                    .baseUrl(new StringBuilder(API_SERVER).append("/cnzn/").toString())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(FastJsonConvertFactory.create())
                    .client(mOkHttpClient)
                    .build();
        }

    }

    /**
     * 获取Retrofit对象
     *
     * @return
     */
    public static Retrofit getRetrofit() {
        if (null == mRetrofit) {
            if (null == mOkHttpClient) {
                mOkHttpClient = OkHttp3Utils.getOkHttpClient();
            }
            mRetrofit = new Retrofit.Builder()
                    .baseUrl(new StringBuilder(API_SERVER).append("/cnzn/").toString())
                    .addConverterFactory(FastJsonConvertFactory.create())//GsonConverterFactory.create()HttpRequestConverterFactory.create()
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(mOkHttpClient)
                    .build();
        }
        return mRetrofit;
    }

}
